﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.UserControls;

    public class PortList : BasePage
    {
        protected HtmlInputButton btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnNewSearch;
        protected Button btnReSearch;
        protected HtmlForm F2562;
        protected GridView gvPort;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblMsg;
        protected Label lblPort;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2PortList(PortQueryEntity portQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(portQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = BaseInfoServiceClientProxy.GetInstance().GetInfoByQueryEntity(portQuery, "PORT");
            base.BindData2GridView(this.gvPort, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPort, "PORT_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请至少选择一项进行删除操作";
            }
            else
            {
                try
                {
                    this.lblMsg.Text = "删除港口" + BaseInfoServiceClientProxy.GetInstance().Delete(selectedID, "PORT") + "成功!";
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                    return;
                }
                PortQueryEntity portQuery = this.GetPortQueryEntity();
                this.BindData2PortList(portQuery);
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPort, "PORT_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = "请选择一项进行编辑";
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = "只能选择一项进行编辑";
            }
            else
            {
                base.Response.Redirect(string.Format("Port.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PortQueryEntity portQuery = this.GetPortQueryEntity();
            portQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = portQuery.CurrentPage;
            portQuery.CurrentPage = "0";
            this.gvPort.Columns[1].Visible = false;
            this.gvPort.AllowSorting = false;
            this.BindData2PortList(portQuery);
            this.hdnActionID.Value = "";
            this.gvPort.ShowFooter = false;
            portQuery.IsGetAll = false;
            portQuery.CurrentPage = currentPageIndex;
            this.SetPortQueryEntity(portQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("Port.xls", this.gvPort);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PortQueryEntity portQuery = this.GetPortQueryEntity();
            portQuery.CurrentPage = "0";
            this.BindData2PortList(portQuery);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PortQueryEntity portQuery = this.GetPortQueryEntity();
            portQuery.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2PortList(portQuery);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PortQueryEntity portQuery = this.GetPortQueryEntity();
            if (portQuery != null)
            {
                portQuery.PortID = "";
                portQuery.PortName = "";
                portQuery.Remark = "";
                portQuery.IsActive = "";
            }
            this.BindData2PortList(portQuery);
        }

        protected void btnNewSearch_Clik(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("PortQuery.aspx?action={0}", base.UrlEncode(this.hdnActionID.Value)));
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PortQueryEntity portQuery = this.GetPortQueryEntity();
            if (int.Parse(portQuery.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                portQuery.CurrentPage = (int.Parse(portQuery.CurrentPage) + 1).ToString();
                this.BindData2PortList(portQuery);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PortQueryEntity portQuery = this.GetPortQueryEntity();
            if (int.Parse(portQuery.CurrentPage) > 0)
            {
                portQuery.CurrentPage = (int.Parse(portQuery.CurrentPage) - 1).ToString();
                this.BindData2PortList(portQuery);
            }
        }

        private PortQueryEntity GetPortQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as PortQueryEntity);
            }
            return (base.PageQueryEntitySession as PortQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PortQueryEntity portQuery = this.GetPortQueryEntity();
                portQuery.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2PortList(portQuery);
            }
        }

        protected void gvPort_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblREMARK"));
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblPORT_NAME"));
                }
            }
        }

        protected void gvPort_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            this.hdnRtnValue.Value = base.UrlReplace(this.gvPort.DataKeys[e.NewSelectedIndex]["PORT_ID"].ToString());
            if (!string.IsNullOrEmpty(this.hdnRtnValue.Value))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + this.hdnRtnValue.Value + "';  window.close();</script>", false);
            }
        }

        protected void gvPort_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PortQueryEntity portQuery = this.GetPortQueryEntity();
                portQuery.SortField = e.SortExpression;
                if (portQuery.SortDirection == "ASC")
                {
                    portQuery.SortDirection = "DESC";
                }
                else if (portQuery.SortDirection == "DESC")
                {
                    portQuery.SortDirection = "ASC";
                }
                this.BindData2PortList(portQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PortQueryEntity portQuery = new PortQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    portQuery.PortID = this.PreviousPage.PortID;
                    portQuery.PortName = this.PreviousPage.PortName;
                    portQuery.Remark = this.PreviousPage.Remark;
                    portQuery.IsActive = this.PreviousPage.IsActive;
                }
                else
                {
                    portQuery.PortID = "";
                    portQuery.PortName = "";
                    portQuery.Remark = "";
                    portQuery.IsActive = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            portQuery.IsGetAll = false;
            portQuery.CurrentPage = "0";
            portQuery.PageSize = this.gvPort.PageSize.ToString();
            portQuery.SortField = "PORT_ID";
            portQuery.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvPort.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetPortQueryEntity(portQuery);
            this.BindData2PortList(portQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.gvPort.Columns[1].Visible = false;
                this.gvPort.Columns[2].Visible = true;
                this.btnDelete.Visible = false;
                this.btnEdit.Visible = false;
                this.btnExport.Visible = false;
                this.btnList.Visible = false;
                this.btnNew.Visible = false;
                this.btnReSearch.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPortQueryEntity(PortQueryEntity portQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = portQuery;
            }
            else
            {
                base.PageQueryEntitySession = portQuery;
            }
        }

        public PortQuery PreviousPage
        {
            get
            {
                return (PortQuery) base.PreviousPage;
            }
        }
    }
}

